
cap cd "D:\Dropbox\RCT on corruption & productivity\Replication"
cap cd "C:\Users\ejm5\Dropbox\RCT on corruption & productivity\Replication"

/*CODING DECISIONS INVOLVING IDENTIFIED DATA WERE REMOVED TO PROTECT CONFIDENTIALITY OF RESPONDENTS. FIRMS THAT TOOL COURSE BUT DID NOT CONSENT TO SURVEY WERE DROPPED FROM RAW DATA. FOR VERIFICATION PLEASE CHECK DIRECTLY WITH AUTHORS.*/


/*Main Data*/
use "DUKE_RESTAURANT_ENDLINE_MERGED_GGSHEEAT_RAW2", clear
set scheme plottig

/*Courses*/
encode vietcourse_class_name, gen(treatment)

/*Code Treatment Variable Names*/
generate treatment_dich=0 if treatment==2
replace treatment_dich=1 if treatment==1|treatment==3
label variable treatment "Random course assignment"
label values treatment treatment2
label define treatment2 1 "Internal Controls" 2 "Marketing (Placebo)" 3 "Restaurant Management"

/*Dichotomous Treatment*/
label variable treatment_dich "Management or Internal Controls=1"
lab values treatment_dich treatment_dich
lab define treatment_dich 1 "Treatment" 0 "Placebo"

/*Internal Controls Course*/
generate IC=1 if treatment==1
replace IC=0 if treatment !=1
lab var IC "Internal Controls Class=1"

/*Management Course*/
generate MBA=1 if treatment==3
replace MBA=0 if treatment !=3
lab var MBA "Restaurant Management Class=1"


/*Marketing*/
generate marketing=1 if treatment==2
replace marketing=0 if treatment !=2
lab var marketing "Marketing Class=1"


/*Code Survey Wave*/
encode vietcourse_class_name, gen(course)
split date_marked_as_complete, generate(time) parse(-)

destring time1, gen(year) force
destring time2, gen(month) force
generate wave2=0 if year==2023 & month<=8
replace wave2=1 if year==2023 & month>8
replace wave2=1 if year==2024 & month<4
replace wave2=2 if year==2024 & month>=4
drop if wave2==.

******************************************************************************************************************
/*Top Code Out of Range Data*/

tab sec1_q15

/*One firm appears confused about tax rate. We correct the mistake to avoid outlier*/
hist sec1_q18
hist sec1_q19


foreach var in sec1_q1 sec1_q2 sec1_q3 sec1_q4 sec1_q5 sec1_q6 sec1_q7 sec1_q8 sec1_q11 sec1_q12 sec1_q13 sec1_q14 sec1_q15 sec1_q16 sec1_q17 sec1_q17_2 sec1_q18 sec1_q19 sec1_q20 sec1_q21_confirm sec1_q21 sec1_q25 sec1_q27 sec1_q27b{
	replace `var'=0 if `var'==.
	sum `var', detail
	replace `var' =r(p95) if `var' ==9999   /*Top Code High Mising Values*/
	replace `var' =r(p5) if `var' ==9990	/*Bottom Code Low Missing Values*/
	replace `var'= (`var'*1000000)/24385  /*Convert to USD*/
}


/*replace `var' =.b if `var' ==9999|`var' ==9990*/
*****************************************************************************************************************

/*Create Dichotomous Bribes*/
generate bribe_dich=1 if sec1_q15>0
replace bribe_dich=0 if bribe_dich==.
generate bribe_dich2=bribe_dich*100
lab var bribe_dich "Paid Any Bribe at All=1"

generate ln_bribe=ln(bribe_dich+.01)
lab var ln_bribe "Bribe Amount (USD, ln)"


/*Course Completition*/
generate completed=1 if vietcourse_acc=="MBA576"|vietcourse_acc=="MKT630"|vietcourse_acc=="MKT354"|vietcourse_acc=="MKT325"|vietcourse_acc=="MKT309"|vietcourse_acc=="IC1698"|vietcourse_acc=="IC1533"|vietcourse_acc=="IC1316"|vietcourse_acc=="MKT1497"|vietcourse_acc=="MBA1355"|vietcourse_acc=="MBA1692"|vietcourse_acc=="MBA1168"|vietcourse_acc=="MBA1035"|vietcourse_acc=="MBA1775"|vietcourse_acc=="MBA1585"|vietcourse_acc=="IC12581"|vietcourse_acc=="IC12585"
replace completed=0 if completed==.


/*Major City*/
generate city=1 if province_id==2|province_id==16|province_id==3|province_id==13|province_id==9|province_id==17
replace city=0 if city==.
generate hanoi=1 if province==2
replace hanoi=0 if province !=2
generate hcmc=1 if province==1
replace hcmc=0 if province!=1
label var city "Restaurant in National City"


/*South of 17th Parallel*/
generate south=1 if province_id==1|province_id==2|province_id==3|province_id==4|province_id==5|province-id==6|province_id==7|province_id==9|province_id==10|province_id==11|province_id==13|province_id==17|province_id==18|province_id==19|province_id==21|province_id==23|province_id==27
replace south=0 if south==.
label var south "Restaurant in South"


/*Regulatory Experience - Inspections*/

egen inspections=rowtotal(sec2_q28*)
lab var inspections "Total number of inspections firm experienced"

egen fines=rowtotal(sec2_q30*)
lab var inspections "Total number of fines during inspections"

*****************************************************************************************************************

/*UCT Question 2 - Bribes During Inspections*/
generate activities2= sec2_q39a
replace activities2=sec2_q39b if sec2_q39a>4
generate UCT2=1 if sec2_q39a<=4
replace UCT2=0 if sec2_q39b<=4
label var activities2 "Activities in Second UCT (Inspections)"
label var UCT2 "Sensitive list=1"
lab values UCT2 UCT
lab define UCT 1 "Sensitive" 0 "Non-Sensitive"

/*UCT Question 2 - Bribes During Inspections Amount*/
destring sec2_q40a, replace
destring sec2_q40b, replace
generate inspection_expenditures=sec2_q40a
replace inspection_expenditures=sec2_q40b if sec2_q40a==.
replace inspection_expenditures=5 if inspection_expenditures>5 & inspection_expenditures<10000
replace inspection_expenditures=(inspection_expenditures*1000000)/24385

generate UCT4=1 if sec2_q40a<=5
replace UCT4=0 if sec2_q40b<=5
label var UCT4 "Sensitive list=1"
lab values UCT4 UCT

/*UCT Question 1 - General Bribes*/
generate activities= sec2_q35a
replace activities=sec2_q35b if sec2_q35a>4
generate UCT=1 if sec2_q35a<=4
replace UCT=0 if sec2_q35b<=4
label var activities "Activities in First UCT (General Activities)"
label var UCT "Sensitive list=1"
lab values UCT UCT

/*UCT Question 2 - Beneral Bribes Amount*/
destring sec2_q36a, replace
destring sec2_q36b, replace
generate general_expenditures=sec2_q36a
replace general_expenditures=sec2_q36b if sec2_q36a==.
replace general_expenditures=200 if general_expenditures>5 & general_expenditures<10000
replace general_expenditures=(general_expenditures*1000000)/24385

generate UCT3=1 if sec2_q36a<=200
replace UCT3=0 if sec2_q36b<=200
label var UCT3 "Sensitive list=1"
lab values UCT3 UCT

*****************************************************************************************************************
/*Generate Productivity*/

sum sec1*

gen exp_inv=sec1_q5
gen inv_used=sec1_q5*(sec1_q6/100)
gen inv_remain=sec1_q5*(sec1_q7/100)
gen inv_spoilt=sec1_q5*(sec1_q8/100)
gen exp_ingred_mtrls=sec1_q11
gen inv_carryover=exp_ingred_mtrls*[(inv_remain/inv_used)*.75]
replace inv_carryover=0 if inv_carryover==.
gen exp_util=sec1_q12
gen exp_wage=sec1_q13
gen exp_ownsal=sec1_q14
gen exp_consumables=exp_ingred_mtrls-inv_carryover
gen exp_rent=sec1_q16
gen exp_interest=sec1_q17_2
gen exp_vat=sec1_q18
gen exp_cit=sec1_q19
gen exp_om=sec1_q20
gen exp_other=sec1_q21

gen exp_total=inv_used+inv_spoilt+inv_carryover+exp_consumables+exp_util+exp_wage+exp_ownsal+exp_rent+exp_interest+exp_om+exp_other
label var exp_total "Expenditures Total"
generate profit=sec1_q2-exp_total
lab var profit "Profit Calculated from Workbook"

gen exp_total_bribes=exp_total+sec1_q15

generate profit_margin=(profit/sec1_q2)
lab var profit_margin "Profit Margin Calculated from Workbook"

generate exp_employees=exp_wage/500
lab var exp_employees "Number of Workers"

gener lp=profit/exp_employees
lab var lp "Value Added Per Worker"



/*Regulatory Knowledge*/
gen correct_signage=1 if sec1_q32==3|sec1_q32==4
replace correct_signage=0 if sec1_q32==1|sec1_q32==2
lab var correct_signage "Knows fine for incorrect fire prevention signage"

gen correct_explosion=1 if sec1_q33==3|sec1_q33==4
replace correct_explosion=0 if sec1_q33==1|sec1_q33==2|sec1_q33==4
lab var correct_explosion "Knows fine for possessing explosive materials"

gen correct_shelflife=1 if sec1_q34==2
replace correct_shelflife=0 if sec1_q34==1|sec1_q34==3|sec1_q34==4
lab var correct_shelflife "Knows correct shelflife"

egen knowledge=rowtotal(correct*)

generate picture1=1 if sec3_q43 !=""
replace picture1=0 if sec3_q43==""
generate picture2=1 if sec3_q44 !=""
replace picture2=0 if sec3_q44==""
generate picture3=1 if sec3_q45 !=""
replace picture3=0 if sec3_q45==""
generate picture4=1 if sec3_q46 !=""
replace picture4=0 if sec3_q46==""
generate picture5=1 if sec3_q47 !=""
replace picture5=0 if sec3_q47==""
egen pictures_no=rowtotal(picture1 picture2 picture3 picture4 picture5)

drop id version time1 time2 time3 graduate exp_inv inv_used inv_remain inv_spoilt exp_ingred_mtrls inv_carryover exp_util exp_wage exp_ownsal exp_consumables exp_rent exp_interest exp_vat exp_cit exp_om exp_other picture1 picture2 picture3 picture4 picture5 


lab var type "Type of restaurant"
lab var endline "Completed endline survey"
lab var female "Manager is female"
lab var lessons "Number of course lessons completed"
lab var completed "Received graduation certificate"
lab var year "Year of endline survey"
lab var month "Month of endline survey"
lab var wave2 "Wave of survey: 0=initial"
lab var bribe_dich2 "Bribe dich rescaled to 100 for graph"
lab var hanoi "Restaurant in Hanoi"
lab var hcmc "Restaurant in Ho Chi Minh City"
lab var south "Restaurant south of 17th parallel"
lab var fines "Total number of fines during inspection"
lab var inspection_expenditures "Expensitures on UCT inspections list"
lab var general_expenditures "Expenditures on UCT general list"
lab var exp_total_bribes "Workbook expenditures including bribes"
lab var exp_total "Expenditures total from workboook"
lab var knowledge "Knowledge test of regulation"
lab var pictures_no "Total number of pictures submitted"
lab var submission_date "Submission time and date"
lab var date_marked_as_complete "Survey date marked as completed"



save "DUKE_RESTAURANT_ENDLINE_MERGED_WORKING.dta", replace

********************************************************************************************************











